package cn.zc.student.service.service.impl;

import cn.tools.dozer.config.DozerUtils;
import cn.zc.student.entity.Dto.StudentExercisesResultDto;
import cn.zc.student.entity.StudentExercisesResult;
import cn.zc.student.service.mapper.StudentExercisesResultMapper;
import cn.zc.student.service.service.StudentExercisesResultService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 学生练习题成绩 服务实现类
 * </p>
 *
 * @author keno
 * @since 2022-02-23
 */
@Service
public class StudentExercisesResultServiceImpl extends ServiceImpl<StudentExercisesResultMapper, StudentExercisesResult> implements StudentExercisesResultService {
    @Resource
    private DozerUtils dozerUtils;

    /**
     * 查询学生练习题分数
     * @param menuId
     * @param studentId
     * @return
     */
    @Override
    public StudentExercisesResultDto findStudentExercisesResult(String menuId, String studentId) {
        StudentExercisesResult one = getOne(new QueryWrapper<StudentExercisesResult>().lambda()
                .eq(StudentExercisesResult::getMenuId, menuId).eq(StudentExercisesResult::getStudentId, studentId));
        return dozerUtils.map(one,StudentExercisesResultDto.class);
    }


    /**
     * 创建学生练习题分数
     * @param menuId
     * @param studentId
     * @param score
     * @return
     */
    @Override
    public boolean createStudentExercisesResult(String menuId, String studentId, String score) {
        StudentExercisesResult studentExercisesResult = new StudentExercisesResult();
        studentExercisesResult.setMenuId(menuId);
        studentExercisesResult.setStudentId(studentId);
        studentExercisesResult.setScore(score);
        /**
         * 查询这个目录是否有数据，若是有则修改，若无则创建
         */
        StudentExercisesResult one = getOne(new QueryWrapper<StudentExercisesResult>().lambda().eq(StudentExercisesResult::getMenuId, menuId).eq(StudentExercisesResult::getStudentId, studentId));
        if(one == null){
            return save(studentExercisesResult);
        }

        studentExercisesResult.setId(one.getId());

        return updateById(studentExercisesResult);
    }
}
